(defpackage :register
  (:use :accounts :common-lisp :cl-who :ht-simple-ajax :hunchentoot)
  (:export :load-register-page))

(in-package :register)

(hunchentoot:define-easy-handler (register-page :uri "/register") ()
  (cl-who:with-html-output-to-string (*standard-output* nil :prologue t)
    (:html :xmlns "http://www.w3.org/1999/xhtml"
     (:head
      (:title "Register in turnbasedgame.")
;      (princ (generate-prologue *ajax-processor*))
      (:script :type "text/javascript" "
//<![CDATA[ 

function fetchURI(uri, callback) {
  var request;
  if (window.XMLHttpRequest) { request = new XMLHttpRequest(); }
  else {
    try { request = new ActiveXObject(\"Msxml2.XMLHTTP\"); } catch (e) {
      try { request = new ActiveXObject(\"Microsoft.XMLHTTP\"); } catch (ee) {
        request = null;
      }}}
  if (!request) alert(\"Browser couldn't make a request object.\");

  request.open('GET', uri, true);
  request.onreadystatechange = function() {
    if (request.readyState != 4) return;
    if (((request.status>=200) && (request.status<300)) || (request.status == 304)) {
      var data = request.responseXML;
      if (callback!=null) { callback(data); }
    }
    else { 
      alert('Error while fetching URI ' + uri);
    }
  }
  request.send(null);
  delete request;
}

function ajax_call(func, callback, args) {
  var uri = '/ajax/' + encodeURIComponent(func) + '/';
  var i;
  if (args.length > 0) {
    uri += '?'
    for (i = 0; i < args.length; ++i) {
      if (i > 0) { uri += '&' };
      uri += 'arg' + i + '=' + encodeURIComponent(args[i]);
    }
  }
  fetchURI(uri, callback);
}

function ajax_check_username(username, callback) {
    ajax_call('CHECK-USERNAME', callback, [username]);
}
function ajax_check_password (password, callback) {
    ajax_call('CHECK-PASSWORD', callback, [password]);
}

function ajax_register_user (username, password, email, firstname, lastname, callback) {
    ajax_call('REGISTER-USER', callback, [username, password, email, firstname, lastname]);
}

function checkUsernameCallback(response) {
     if (response.firstChild.firstChild.nodeValue == \"false\") {
      document.getElementById('picUsername').src='http://www.2020name.com/images/ok.png'; 
     }
     else {
    
     document.getElementById('picUsername').src='http://caledonii.world-of-dungeons.net/wod/css/icons/common/not_ok.png'; 
     }
 
}

function checkPasswordCallback(response) {
   if (response.firstChild.firstChild.nodeValue == \"true\") {
      document.getElementById('picPassword').src='http://www.2020name.com/images/ok.png'; 
  }
  else {
     document.getElementById('picPassword').src='http://caledonii.world-of-dungeons.net/wod/css/icons/common/not_ok.png'; 
  }
}

function registerUserCallback(response) {
    if (response.firstChild.firstChild.nodeValue == \"true\") {
//      alert('User has been registered');
       window.location = \"/game\";
      }
    else {
      alert('User has not been registered. Verify entered data.');
    }
}

// calls our Lisp function with the value of the text field
function checkUsername() {
  ajax_check_username(document.getElementById('username').value, checkUsernameCallback);
}
function checkPassword() {
ajax_check_password(document.getElementById('password').value, checkPasswordCallback);
}

function registerUser() {
   if (document.getElementById('username').value == \"\") {
       alert('Please provide username');
       return;
   }

   if (document.getElementById('password').value == \"\") {
       alert('Please provide password');
       return;
   }
   if (document.getElementById('email').value == \"\") {
       alert('Please provide email address');
       return;
   }

   firstname=\"Noob\";
   if (document.getElementById('firstname').value != \"\") {
        firstname=document.getElementById('firstname').value;
   }
   lastname=\"Noobson\";
   if (document.getElementById('lastname').value != \"\") {
        lastname=document.getElementById('lastname').value;
   }
       
   ajax_register_user(document.getElementById('username').value, document.getElementById('password').value, document.getElementById('email').value, firstname, lastname, registerUserCallback);
}


    
"))
     (:body
       "<form action=processLogin>
       Username: <input type=\"text\" id=\"username\" onChange=\"checkUsername()\"/> 

       <img src=\"http://www.odry.ru/pics/button_map.gif\" id=\"picUsername\"/>
       <br/>
       Password: <input type=\"password\" id=\"password\" onChange=\"checkPassword()\" /> 
       <img src=\"http://www.odry.ru/pics/button_map.gif\" id=\"picPassword\"/>
       <br />
       Email: <input type=\"email\" id=\"email\"/><br/>
       First name: <input type = \"text\" id = \"firstname\"/><br/>
       Last name: <input type = \"text\" id=\"lastname\"/><br/>
       <input type=\"button\" id='submitRegister' value=\"Register\" onClick=\"registerUser()\"/>

       </form>"
  ))))